#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pii pair<int,int>
#define pb push_back
#define vec vector<int>
#define y1 y_____hahahaha_____1

using namespace std;

typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;

int read(){int x = 0,sgn = 1;char ch = getchar();for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;for(;isdigit(ch);ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48); return x * sgn;}

const int N = 3e5+10;

int dfn[N],R[N],tim,now,ans,n;
vec G1[N],G2[N];
set<pii> s;

void dfs(int x){dfn[x] = ++tim; for(auto y : G2[x]) dfs(y); R[x] = tim;}

bool intree(int x,int y){return dfn[x] <= dfn[y]  && dfn[y] <= R[x];}

void dfs1(int x){
	int lst = now; 
	auto it = s.lower_bound(pii(dfn[x],0));
	if(it != s.end()) now += 1 - intree(x,it->se);
	if(it != s.begin()){
		auto iit = it--; now += 1 - intree(it->se,x);
		if(iit != s.end()) now -= 1 - intree(it->se,iit->se);
	}
	ans = max(ans,now); s.insert(pii(dfn[x],x));for(auto y : G1[x]) dfs1(y); s.erase(pii(dfn[x],x)); now = lst; return;
}

void solve(){
	n = in; for(int i = 1;i <= n;i++) G1[i].clear(),G2[i].clear();
	for(int i = 2,f;i <= n;i++) f = in,G1[f].pb(i);
	for(int i = 2,f;i <= n;i++) f = in,G2[f].pb(i);
	tim = 0;dfs(1);  now = 0; ans = 0; dfs1(1); s.clear(); printf("%d\n",ans + 1);
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	for(int t = in;t;t--) solve();
	return 0;
}
